package _022_2_12

import "sort"

func findInsertIndex(visited []int, lastIndex int) int {
	searchNum := 0
	index := lastIndex + 1
	index %= len(visited)
	for true {
		if visited[index] != -1 {
			visited[index] = -1
			searchNum++
			if searchNum == 2 {
				break
			}
			index++
			index %= len(visited)
		}
	}
	return index
}

func deckRevealedIncreasing(deck []int) []int {
	sort.Ints(deck)

	insertedIds := make([]int, len(deck))
	result := make([]int, len(deck))
	if len(deck) <= 0 {
		return nil
	}
	insertedIds[0] = -1
	result[0] = deck[0]
	for i := 0; i < len(insertedIds); i++ {
		index := findInsertIndex(insertedIds, 0)
		result[index] = deck[i]
	}
	return result
}
